<template>
  <!-- :file-list 展现的图片或者文件地址 -->
  <div>
    <!-- action 自动上传的地址 -->
    <el-upload
      :file-list="fileList"
      list-type="picture-card"
      action="#"
      :on-preview="preview"
      :class="{disabled: fileComputed }"
      :on-remove="handleRemove"
      :on-change="handleChange"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />

    </el-upload>
    <!-- 进度条 -->
    <el-progress v-if="showPercent" style="width: 180px" :percentage="percent" />

    <el-dialog title="图片预览" :visible.sync="showDialog">
      <img :src="imgUrl" style="width: 100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'

export default {
  props: { // props只读的
    limit: {
      type: Number, // 数字类型
      default: 1 // 只传一张
    }
  },
  data() {
    return {
      fileList: [], // { url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2Fe%2F59%2Ff80e560851.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641697948&t=881c333c88dbbe5dd48295dadc097a76' }, { url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F092920114119%2F200929114119-3-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641698457&t=1cd2340ed775d46ef8e3bad609ebac1c' }
      showDialog: false,
      imgUrl: '',
      showPercent: false,
      percent: 0
    }
  },
  computed: {
    fileComputed() { // 布尔值
      return this.fileList.length === this.limit
    }
  },
  methods: {
    preview(file) { // 显示
      // console.log(file)
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove(file, fileList) { // 移除
      // this.fileList = fileList // 都可以
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    handleChange(file, fileList) { // 添加
      // this.fileList = fileList
      // this.fileList = [...fileList]
      this.fileList = fileList.map(item => item)
    },
    beforeUpload(file) {
      // console.log(file)
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png', 'image/jpeg'] // 限定类型
      if (!types.some(item => item === file.type)) {
        this.$message.warning('上传图片只能是jpeg,gif,bmp,png,jpeg格式')
        return false
      }
      // 限制图片大小Byte字节 2M = 2 * 1024 * 1024
      const maxSize = 2 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.warning('图片大小不能超过2M')
        return false
      }
      return true
    },
    upload(params) {
      this.showPercent = true
      // 要将file传到腾讯云
      var cos = new COS({
        SecretId: 'AKID74kAJBm7AwRgeLlulLegRv84i5AyWmlT', // 身份识别 ID
        SecretKey: 'SdW8TXKp30f39uPcYDbiX5MM5WgY9SUF' // 身份密钥
      })
      cos.putObject({
        Bucket: 'zhibin-1308725966', // 存储桶
        Region: 'ap-chengdu', // 地域
        Key: params.file.name, // 文件名
        Body: params.file, // 要上传的文件对象
        StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
        // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
        onProgress: (params) => {
          // console.log(params) 拿到当前进度百分比
          this.percent = params.percent * 100
        }
      }, (err, data) => {
        // console.log(err || data)
        // 首先判断成功
        if (!err && data.statusCode === 200) {
          this.fileList = this.fileList.map(item => {
            if (item.uid === params.file.uid) { // uid为图片的唯一标识
              return { url: 'http://' + data.Location, status: 'success' } // el-upload 会检测地址
            }
            return item
          })
          setTimeout(() => {
            this.showPercent = false
            this.percent = 0
          }, 200)
        }
      })
    }
  }

}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none
}

</style>
